package org.jeecg.modules.wlhy.utils;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.wlhy.hybank.entity.HyBank;
import org.jeecg.modules.wlhy.hybank.service.IHyBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BankTypeUtil {

    private static Map<String,String> crBankNoList = new HashMap<>();
    private static Map <String ,String> bankMaps = new HashMap <>();
    private static Map <String ,String> bankName2Maps = new HashMap <>();
    private static Map <String ,String> bankInfoMaps = new HashMap <>();

    private static IHyBankService _bankService;
    private static boolean isClustered;
    public BankTypeUtil(@Autowired IHyBankService bankService){
        _bankService = bankService;
    }

    @Value(value="${params.isClustered}")
    public void setClustered(boolean st) {
        isClustered = st;
    }

    //根据银行类型找行号
    public static String getBankNo(String bankType){
        if(StringUtils.isEmpty(bankType)){
            return "";
        }
        if(isClustered) {
           HyBank bank = _bankService.getByBankType(bankType);
           if(bank != null){
               return bank.getBankHh();
           }
        }else {
            if(crBankNoList.containsKey(bankType)){
                return crBankNoList.get(bankType);
            }
        }
        return "";
    }

    public static String getBankTypeByBankName(String bankName){
        if(StringUtils.isEmpty(bankName)){
            return "";
        }
        if(isClustered) {
            HyBank bank = _bankService.getByBankName(bankName);
            if(bank != null){
                return bank.getBankType();
            }
        }else {
            if (bankMaps.containsKey(bankName)) {
                return bankMaps.get(bankName);
            }
        }
        return "";
    }

    public static String getBankTypeByBankName2(String bankName2){
        if(StringUtils.isEmpty(bankName2)){
            return "";
        }
        if(isClustered) {
            HyBank bank = _bankService.getByBankName2(bankName2);
            if(bank != null){
                return bank.getBankType();
            }
        }else {
            if (bankName2Maps.containsKey(bankName2)) {
                return bankName2Maps.get(bankName2);
            }
        }
        return "";
    }

    public static boolean checkBankType(String bankType){
        if(StringUtils.isEmpty(bankType)){
            return false;
        }
        if(isClustered) {
            HyBank bank = _bankService.getByBankType(bankType);
            return bank != null;
        }else {
            return bankInfoMaps.containsKey(bankType);
        }
    }

    public static String getBankNameByBankType(String bankType){
        if(StringUtils.isEmpty(bankType)){
            return "";
        }
        if(isClustered) {
            HyBank bank = _bankService.getByBankType(bankType);
            if(bank != null){
                return bank.getBankName();
            }
        }else {
            if (bankInfoMaps.containsKey(bankType)) {
                return bankInfoMaps.get(bankType);
            }
        }
        return "";
    }

    public static void initBankType(List<HyBank> bankList) {

        crBankNoList.clear();
        bankMaps.clear();
        bankInfoMaps.clear();
        bankName2Maps.clear();
        for (HyBank item : bankList) {
            String bankName = item.getBankName();
            String bankType = item.getBankType();
            String bankName2 = item.getBankName2();
            String bankHh = item.getBankHh();
            crBankNoList.put(bankType,bankHh);
            bankMaps.put(bankName ,bankType);
            bankInfoMaps.put(bankType,bankName);
            bankName2Maps.put(bankName2,bankType);
        }
    }

}
